<template>
	<view>
		<view class="">
			<view class="margin-lr margin-top" style="position: relative;border-radius: 10upx;overflow: hidden;">
				<image src="../static/vip.png" style="width: 100%;height: 250rpx;"></image>
				<view class=" u-flex u-p-l-30 u-p-t-80 u-p-b-30" style="position: absolute;top: 0;width: 100%;">
					<view class="u-m-r-20">
						<u-avatar :src="avatar" size="100"></u-avatar>
					</view>
					<view class="u-flex-1 ">
						<view class="u-font-18 text-white text-bold" style="color: #C59D7C;">{{userName}}</view>
						<view class="u-font-14 u-m-t-10 u-tips-color" style="color: #C59D7C;" v-if="!isVip">您目前还未开通会员
						</view>

						<view class="u-font-14 u-m-t-10 u-tips-color" style="color: #C59D7C;" v-if="isVip&&vipEndTime">
							会员到期:{{vipEndTime}}</view>
					</view>
				</view>
			</view>
			<view class="flex margin-top-sm flex-wrap padding-lr-sm">
				<view v-for="(item,index) in vipList" :key='index' @click="select(item)" class=""
					style="display: inline-block;width: 216rpx;height: 200rpx;margin: 10rpx 10rpx 0rpx;">
					<view class="text-center flex flex-direction justify-center padding-tb radius"
						:class="selNum == item.id?'active': ''"
						style="color: #DFC5A7;width: 216rpx;height: 200rpx;background: linear-gradient(-30deg, #2B2A30, #4A4A4A);border: 1px;">
						<view class="text-bold">{{item.vipName}}</view>
						<view class="text-bold" style="margin-top: 20upx;">¥<text class="text-xxl">{{item.money}}</text>
						</view>
						<!-- <view>立即预约</view> -->
					</view>
				</view>
			</view>
			<view class="padding-tb radius margin-top margin-lr"
				style="background-color: #343339;border-radius: 10upx;padding-bottom: 60rpx;">
				<view class="text-center text-xl text-bold " style="color: #CAB49C;">开通专享特权</view>
				<view class="flex  flex-wrap">
					<view v-for="(item,index) in MemberList" :key="index"
						style="width: 33%;text-align: center;margin-top: 34upx;">
						<image :src="item.memberImg" mode="" style="margin: 0 auto;height: 45rpx;width: 45rpx;"></image>
						<view class="grid-text margin-top-sm" style="color: #DFC5A7;">{{item.memberName}}</view>
					</view>
				</view>
			</view>
		</view>
		<view style="height: 110rpx;"></view>
		<view class="flex justify-between cu-bar foot bg padding-lr" style="padding-top: 20rpx;padding-bottom: 20rpx;"
			v-if="!isVip">
			<view style="color: #DFC5A7;">
				实付：<text style="font-size: 38upx;margin-top: 8upx;">{{price}}</text>元
			</view>
			<view class="">
				<u-button :custom-style="customStyle" @click="showPay = true" shape="circle"
					:hair-line="false">立即开通</u-button>
			</view>
		</view>


		<u-popup v-model="showPay" mode="bottom" border-radius="14" :closeable="true">
			<view
				style="width: 100%;text-align: center;font-size:38rpx;font-weight: bold;margin-top:15rpx;margin-bottom: 80rpx;">
				选择支付方式
			</view>
			<view style="display: flex;height: 100upx;align-items: center;padding: 20upx 30rpx;"
				v-for="(item,index) in openLists" :key='index'>
				<view style="display: flex;width:80%;align-items: center;">
					<image :src="item.image" style="width: 55upx;height: 55upx;border-radius: 50upx;"></image>
					<view style="font-size: 30upx;margin-left: 20upx;width: 70%;">{{item.text}}
					</view>
				</view>
				<view style="width: 20%;display: flex;justify-content: flex-end;">
					<radio-group name="openWay" style="margin-left: 20upx;" @tap='selectWay(item.id)'>
						<label class="tui-radio">
							<radio color="#C59D7C" :checked="openWay === item.id ? true : false" />
						</label>
					</radio-group>
				</view>
			</view>
			<view
				style="width: 690rpx;height: 80rpx;background:#C59D7C;color:#FFFFFF;text-align: center;line-height: 80rpx;border-radius: 50rpx;margin: 30rpx;"
				@tap="pay()">确认支付</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				customStyle: {
					width: '250rpx',
					color: '#402321',
					background: "#DFC5A7",
					border: 0,
					fontWeight: '700'
				},
				gridData: [{
						title: '专享九折',
						image: '../static/1.png'
					},
					// {
					// 	title: '特权礼物',
					// 	image: '../static/2.png'
					// },
					{
						title: '身份标识',
						image: '../static/3.png'
					},
					// {
					// 	title: '超值专享券',
					// 	image: '../static/4.png'
					// },
					// {
					// 	title: '商家特权',
					// 	image: '../static/5.png'
					// },
					// {
					// 	title: '定制挂件',
					// 	image: '../static/6.png'
					// }
				],
				avatar: '',
				userName: '匿名',
				vipList: [],
				selNum: 2,
				newPrice: 0,
				money: 0,
				price: 0,
				MemberList: [],
				isVip: false,
				vipEndTime: '',
				showPay: false,
				openWay:2,
				openLists: [],
			}
		},
		onLoad() {
			this.avatar = uni.getStorageSync('avatar')
			this.userName = uni.getStorageSync('userName')

			this.getVipList()
			this.getMemberList()
			this.getMoney()
			this.getIsVip()
			this.getUserInfo()

			// #ifdef APP-PLUS
			this.openLists = [{
				image: '../../static/images/zhifubao.png',
				text: '支付宝',
				id: 1
			}, {
				image: '../../static/images/my/icon_weixin.png',
				text: '微信',
				id: 2
			}, {
				image: '../../static/images/lingqian.png',
				text: '零钱',
				id: 3
			}];
			this.openWay = 2;
			// #endif

			// #ifdef MP-WEIXIN
			this.openLists = [{
				image: '../../static/images/my/icon_weixin.png',
				text: '微信',
				id: 2
			}, {
				image: '../../static/images/lingqian.png',
				text: '零钱',
				id: 3
			}];
			this.openWay = 2;
			// #endif

			// #ifdef H5
			let ua = navigator.userAgent.toLowerCase();
			if (ua.indexOf('micromessenger') !== -1) {
				//公众号是否自动登录  237
				this.$Request.get('/app/common/type/237').then(res => {
					if (res.data && res.data.value && res.data.value == '是') {
						this.openLists = [{
							image: '../../static/images/my/zhifubao.png',
							text: '支付宝',
							id: 1
						}, {
							image: '../../static/images/my/icon_weixin.png',
							text: '微信',
							id: 2
						}, {
							image: '../../static/images/lingqian.png',
							text: '零钱',
							id: 3
						}];
						this.openWay = 2;
					} else {
						this.openLists = [{
							image: '../../static/images/my/zhifubao.png',
							text: '支付宝',
							id: 1
						}, {
							image: '../../static/images/lingqian.png',
							text: '零钱',
							id: 3
						}];
						this.openWay = 1;
					}
				})
			} else {
				this.openLists = [{
					image: '../../static/images/my/zhifubao.png',
					text: '支付宝',
					id: 1
				}, {
					image: '../../static/images/lingqian.png',
					text: '零钱',
					id: 3
				}];
				this.openWay = 1;
			}
			// #endif

		},
		methods: {
			selectWay(id){
				this.openWay = id
			},
			getUserInfo() {
				this.$Request.get("/app/user/selectUserById").then(res => {
					if (res.code == 0) {
						this.avatar = res.data.avatar ? res.data.avatar : '../../static/logo.png'
						this.vipEndTime = res.data.vipEndTime
						uni.setStorageSync('avatar', res.data.avatar)
					}
				});
			},
			//获取VIP列表
			getVipList() {
				this.$Request.get('/app/VipDetails/selectVipDetails').then(res => {
					if (res.code == 0) {
						this.vipList = res.data
						this.price = this.vipList[0].money
						this.selNum = this.vipList[0].id
					}
				})
			},
			// 获取特权列表
			getMemberList() {
				this.$Request.get('/app/member/selectMemberList').then(res => {
					if (res.code == 0) {
						this.MemberList = res.data
					}
				})
			},
			getIsVip() {
				this.$Request.get("/app/UserVip/isUserVip").then(res => {
					if (res.code == 0) {
						this.isVip = res.data
					}
				});
			},
			// 我的金元
			getMoney() {
				this.$Request.get("/app/userMoney/selectMyMoney").then(res => {
					if (res.code == 0 && res.data) {
						this.money = res.data.money
					}
				});
			},
			select(e) {
				this.selNum = e.id
				this.price = e.money
			},
			pay() {
				this.showPay = false
				uni.showLoading({
					title: '支付中...'
				});
				if (this.openWay == 2) { //微信
					let data = {
						id: this.selNum,
						type: '2'
					}
					this.$Request.get('/app/orders/generateOrder', data).then(res => {
						if (res.code == 0) {
							let that = this
							// #ifdef MP-WEIXIN
							that.$Request.post("/app/wxPay/payMember", {
								ordersId: res.data.ordersId,
								classify: 3
							}).then(red => {
								if (red.code == 0) {
									uni.requestPayment({
										provider: 'wxpay',
										timeStamp: red.data.timestamp,
										nonceStr: red.data.noncestr,
										package: red.data.package,
										signType: red.data.signType,
										paySign: red.data.sign,
										success: function(redd) {
											that.isVip = true
											uni.showToast({
												title: '开通成功！'
											})
											setTimeout(function() {
												uni.navigateBack()
											}, 1000)
										},
										fail: function(err) {
											that.paySel = false;
											uni.hideLoading();
											that.$queue.showToast(
												'支付失败');
										}
									});
								} else {
									that.paySel = false;
									uni.showToast({
										title: red.msg,
										icon: 'none'
									})
								}
								setTimeout(function () {
									uni.hideLoading();
								}, 2000);
							});
							// #endif

							// #ifdef H5
							that.$Request.post("/app/wxPay/payMember", {
								ordersId: res.data.ordersId,
								classify: 2
							}).then(red => {
								if (red.code == 0) {
									that.callPay(red.data);
								} else {
									that.paySel = false;
									uni.showToast({
										title: red.msg,
										icon: 'none'
									})
								}
								setTimeout(function () {
									uni.hideLoading();
								}, 2000);
							});
							// #endif

							// #ifdef APP-PLUS
							that.$Request.post("/app/wxPay/payMember", {
								ordersId: res.data.ordersId,
								classify: 1
							}).then(red => {
								if (red.code == 0) {
									console.log(JSON.stringify(red))
									that.setPayment('wxpay', JSON.stringify(red
										.data));
								} else {
									that.paySel = false;
									uni.showToast({
										title: red.msg,
										icon: 'none'
									})
								}
								setTimeout(function () {
									uni.hideLoading();
								}, 2000);
							});
							// #endif
						}
					})
				} else if (this.openWay == 1) { //支付宝
					let data = {
						id: this.selNum,
						type: '2'
					}
					this.$Request.get('/app/orders/generateOrder', data).then(res => {
						if (res.code == 0) {
							let that = this
							// #ifdef H5
							that.$Request.post("/app/aliPay/payMember", {
								ordersId: res.data.ordersId,
								classify: 5
							}).then(red => {
								if (red.code == 0) {
									const div = document.createElement('div')
									div.innerHTML = red.data //此处form就是后台返回接收到的数据
									document.body.appendChild(div)
									document.forms[0].submit()
								} else {
									that.paySel = false;
									uni.showToast({
										title: red.msg,
										icon: 'none'
									})
								}
								setTimeout(function () {
									uni.hideLoading();
								}, 2000);
							});
							// #endif
							// #ifdef APP-PLUS
							that.$Request.post("/app/aliPay/payMember", {
								ordersId: res.data.ordersId,
								classify:4
							}).then(red => {
								if (red.code == 0) {
									that.setPayment('alipay', red.data);
								} else {
									that.paySel = false;
									uni.showToast({
										title: red.msg,
										icon: 'none'
									})
								}
								setTimeout(function () {
									uni.hideLoading();
								}, 2000);
							});
							// #endif
						}
					})
				} else if (this.openWay == 3) { //零钱
					if (this.money >= this.price) {
						let data = {
							id: this.selNum,
							type: '2'
						}
						this.$Request.get('/app/orders/generateOrder', data).then(ret => {
							if (ret.code == 0) {
								this.$Request.get('/app/userMoney/payTakingOrder', {
									orderId: ret.data.ordersId
								}).then(res => {
									console.log(res.data)
									if (res.code == 0) {
										this.isVip = true
										uni.showToast({
											title: '开通成功！'
										})
										setTimeout(function() {
											uni.navigateBack()
										}, 1000)
									} else {
										uni.showToast({
											title: res.msg
										})
									}
									setTimeout(function () {
										uni.hideLoading();
									}, 2000);
								})
							}
						})
					} else {
						uni.hideLoading();
						uni.showModal({
							title: '提示',
							content: '当前账户余额不足，请充值',
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
									uni.navigateTo({
										url: '/my/wallet/index'
									})
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						})
					}
				}

			},
			callPay: function(response) {
				if (typeof WeixinJSBridge === "undefined") {
					if (document.addEventListener) {
						document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady(response), false);
					} else if (document.attachEvent) {
						document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady(response));
						document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady(response));
					}
				} else {
					this.onBridgeReady(response);
				}
			},
			onBridgeReady: function(response) {
				let that = this;
				if (!response.package) {
					return;
				}
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						"appId": response.appid, //公众号名称，由商户传入
						"timeStamp": response.timestamp, //时间戳，自1970年以来的秒数
						"nonceStr": response.noncestr, //随机串
						"package": response.package,
						"signType": response.signType, //微信签名方式：
						"paySign": response.sign //微信签名
					},
					function(res) {
						if (res.err_msg === "get_brand_wcpay_request:ok") {
							// 使用以上方式判断前端返回,微信团队郑重提示：
							//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
							uni.removeStorageSync('EditAddress')
							that.isVip = true
							uni.showToast({
								title: '开通成功！'
							})
							setTimeout(function() {
								uni.navigateBack()
							}, 1000)
						} else {
							that.paySel = false;
							uni.hideLoading();
						}
						WeixinJSBridge.log(response.err_msg);
					}
				);
			},
			setPayment(name, order) {
				let that = this;
				uni.requestPayment({
					provider: name,
					orderInfo: order, //微信、支付宝订单数据
					success: function(res) {
						that.isVip = true
						uni.showToast({
							title: '开通成功！'
						})
						setTimeout(function() {
							uni.navigateBack()
						}, 1000)
					},
					fail: function(err) {
						that.paySel = false;
						uni.hideLoading();
						console.log(12)
					}
				});
			}
		},
		computed: {

		}
	}
</script>

<style>
	.bg {
		background-color: #343339;
	}

	.btn {
		width: 100%;
		height: 88upx;
		background: linear-gradient(0deg, #af8262 0%, #cab49c 100%);
		border-radius: 44upx;
		text-align: center;
		line-height: 88upx;
		margin-top: 40upx;
		font-size: 34upx;
		font-weight: 600;
		color: #402321;
	}

	.active {
		border: 1px solid #cab49c !important;
		border-radius: ;
		background: black !important;
	}
</style>